// pages/unpaid/unpaid.js
const app = getApp();
import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    time: 30 * 60 * 60 * 1000,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOrderList();
  },
  async cancelOrder(event){
    const id = event.currentTarget.dataset.item.id
    const res = await request({
      url:app.globalData.baseURL + '/app-api/package/trade-order/cancel',
      method:'POST',
      data:{
        "id": id,
        "reason": "前端发现，订单超时",
        "cancelType": 10
      }
    })
  },
  async getOrderList() {
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/trade-order/page?pageNo=${1}&pageSize=${100}&status=${0}`,
      method: "GET",
    });
    if (res.code === 0) {
      const list = res.data.list;
      let datalist = list.map((item) => {
        item.payPrice = (item.payPrice / 100).toFixed(2);
        item.expireTimeFormatted = item.expireTime - Date.now()
        return item;
      });
      this.setData({
        orderList: datalist,
      });
    } else {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
    }
  },
  async cancelPay(event){
    const id = event.currentTarget.dataset.item.id
    wx.showModal({
      title: '提示',
      content: '您确定要取消订单吗？',
      cancelText: "确认取消",
      confirmText: "我再想想",
      cancelColor: "#ff0000",
      complete: (res) => {
        if (res.cancel) {
          this.gocancel(id)
        }
        if (res.confirm) {
          console.log('用户点击取消');
        }
      }
    })
  },
  async gocancel(id){
    const res = await request({
      url: app.globalData.baseURL + '/app-api/package/trade-order/cancel',
      method:'POST',
      data:{
        "id": id,
        "reason": "用户取消",
        "cancelType": 30
      }
    })
    if(res.code == 0){
      wx.showToast({
        title: '取消成功',
        icon: "success"
      })
      this.getOrderList()
    }else{
      wx.showToast({
        title: res.msg,
        icon:"none"
      })
    }
  },
  goback() {
    wx.navigateBack({
      delta: 1,
    });
  },
  async goParticulars(event) {
    const cardId = event.currentTarget.dataset.item.cartNo;
    const price = event.currentTarget.dataset.item.payPrice;
    const payorderid = event.currentTarget.dataset.item.payOrderId;
    wx.navigateTo({
      url: `../awaitPay/awaitPay?cardId=${cardId}&price=${price}&payOrderId=${payorderid}`,
    });
  },
  async gopay(event) {
    const price = event.currentTarget.dataset.item.payPrice * 100;
    const payorderid = event.currentTarget.dataset.item.payOrderId;
    const id = event.currentTarget.dataset.item.id
    wx.navigateTo({
      url: `/pages/paymethod/paymethod?price=${price}&payOrderId=${payorderid}&id=${id}`,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
